package cn.dubito.learning.web.controller;

import cn.dubito.learning.web.controller.dto.Demo;
import cn.dubito.learning.web.controller.dto.Radio;
import cn.dubito.learning.web.controller.dto.User;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liliang
 * @description:
 * @date 2018/1/18.
 */
@RequestMapping("/user")
@RestController
public class UserController {


    /**
     * 获取当前登录的用户信息
     * @param userDetails
     * @return
     */
    @RequestMapping(value = "/me")
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails userDetails) {
        return userDetails;
    }


    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable String id) {
        System.out.println(id);
    }

    @PutMapping("/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult error) {

        if (error.hasErrors()) {
            error.getAllErrors().forEach(e -> {
                        FieldError fieldError = (FieldError) e;
                        String message = fieldError.getField() + " : " + e.getDefaultMessage();
                        System.out.println(message);
                    }
            );
        }
        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());
        user.setId("1");
        return user;
    }

    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult error) {

        if (error.hasErrors()) {
            error.getAllErrors().forEach(e -> System.out.println(e.getDefaultMessage()));
        }
        System.out.println(user.getId());
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());
        user.setId("1");
        return user;
    }

    @GetMapping
    @JsonView(User.UserSimpleView.class)
    public List<User> query() {
        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }

    @GetMapping("/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    @ApiOperation(value = "用户查询服务")
    public User getInfo(@ApiParam(value = "用户ID") @PathVariable(name = "id") String id) {
        System.out.println("进入 getInfo 服务");
        User user = new User();
        user.setUsername("tom");
        return user;
    }

    @PostMapping("/wangdongyu")
    public Demo get() {
        List<Radio> radioList = new ArrayList<>();
        Demo demo = new Demo();
        Radio radio = new Radio();
        radio.setListenNum(4963680);
        radio.setRadioId(199);
        radio.setRadioName("热歌");
        radio.setRadioPic("http://y.gtimg.cn/music/photo/radio/track_radio_199_10_7.jpg");
        radioList.add(radio);
        radio = new Radio();
        radio.setListenNum(571200);
        radio.setRadioId(126);
        radio.setRadioName("新歌");
        radio.setRadioPic("http://y.gtimg.cn/music/photo/radio/track_radio_126_10_3.jpg");
        radioList.add(radio);

        demo.setRadioList(radioList);
        return demo;
    }
}
